/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.pui;

import cef.command.Command;
import cef.command.CommandI;
import cef.command.Model_MLE_Command;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.event.Event;
import cef.event.Model_MLE_Event;
import cef.pui.EventConstructionException;
import cef.pui.EventImpl;
import cef.pui.pob.Builder;
import coalescent.cef.pui.pob.Builders_Coa;
import coalescent.data.K69Data;
import coalescent.data.meta.MetaInf_K69Data;
import coalescent.data.meta.MetaInf_Mngr;
import coalescent.model.K69;
import commons.core.MultiLHoodComputer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Properties:
 * <ul>
 *     <li>{@code coalescent.builder.K69List.theta.min = valid double string as specified in } {@link Double#parseDouble(java.lang.String)}</li>
 *     <li>{@code coalescent.builder.K69List.theta.max = valid double string as specified in } {@link Double#parseDouble(java.lang.String)}</li>
 *     <li>{@code coalescent.builder.K69List.theta.increment = valid double string as specified in } {@link Double#parseDouble(java.lang.String)}</li>
 *     <li>{@code coalescent.builder.K69Data.url = k69data:xml//URL }<a href="coalescent/cef/pui/pob/doc-files/k69data.xml.txt">file format</a>, must accompany the <a href="coalescent/cef/pui/pob/doc-files/k69data.xsd.txt">schema</a></li>
 *     <li>{@code coalescent.builder.K69MultiLHood.implCode = exact-recursion-multi}</li>
 * </ul>
 *
 * @since
 * @version
 * @author Susanta Tewari
 * @history Created on 2/22/13.
 */
public class K69_MLE_EventImpl extends EventImpl implements Model_MLE_Event<K69, K69Data> {

    private final Builder<MultiLHoodComputer<K69, K69Data, K69Data>> multi_LHood_b;
    private final Builder<List<K69>> k69List_b;
    private final Builder<K69Data> k69Data_b;
    private final Builder<MetaInf_Mngr<MetaInf_K69Data>> metaInf_b;

    /**
     * @param props command properties
     * @throws cef.pui.EventConstructionException
     */
    public K69_MLE_EventImpl(final Properties props) throws EventConstructionException {

        super(props);

        super.processEventProperties(getImplementedEventInterfaces());

        multi_LHood_b = Builders_Coa.of_MultiLHoodComputer_K69(props, null);
        k69List_b     = Builders_Coa.of_Models_K69(props, null);
        k69Data_b     = Builders_Coa.of_Data_K69_File(props, null);
        metaInf_b = Builders_Coa.of_MetaInf_Mngr_K69Data_File(props, "coalescent.MetaInf_K69Data",
                "coalescent.MetaInf");
    }

    @Override
    protected List<Class<? extends Event>> getImplementedEventInterfaces() {

        final List<Class<? extends Event>> result = new ArrayList<>(2);

        result.add(Model_MLE_Event.class);
        result.add(Event.class);

        return result;
    }

    @Override
    public List<K69> getModels() {
        return k69List_b.build();
    }

    @Override
    public K69Data getData() {
        return k69Data_b.build();
    }

    @Override
    public MultiLHoodComputer<K69, K69Data, K69Data> getMultiLHoodComputer() {
        return multi_LHood_b.build();
    }

    public MetaInf_Mngr<MetaInf_K69Data> getMetaInf() {
        return metaInf_b.build();
    }

    public static final class CollectExactProb implements CommandOutputListener {

        @Override
        public void receivedEvent(CommandOutputEvent cmdEvent)
                throws CommandOutputListenerException {

            final Model_MLE_Command<K69, K69Data> cmd = (Model_MLE_Command) cmdEvent.getSource();
            final K69_MLE_EventImpl event                    = (K69_MLE_EventImpl) cmd.getEvent();
            final MetaInf_Mngr<MetaInf_K69Data> metaInf_mngr = event.getMetaInf();

            if (metaInf_mngr.isActive()) {

                final MetaInf_K69Data metaInf = metaInf_mngr.hasInfo()
                                                ? metaInf_mngr.load() : metaInf_mngr.getDefault();
                final Map<K69, BigDecimal> lHoods = cmd.getLHoods();

                for (K69 k69 : lHoods.keySet()) {
                    metaInf.addProb(k69.getMutationRate(), lHoods.get(k69));
                }

                metaInf_mngr.store(metaInf);
            }
        }

        @Override
        public void init(Command cmd) {}

        @Override
        public CommandOutputListener newInstance() {
            return new CollectExactProb();
        }

        @Override
        public Class<? extends CommandI> getTargetCommandClass() {
            return Model_MLE_Command.class;
        }
    }
}
